<template>
  <div class="member-order">
    <XtxTabs @tab-click="tabClick" v-model="activeName">
      <XtxTabsPanel v-for="item in orderStatus" :key="item.name" :label="item.label" :name="item.name"></XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div v-if="loading" class="loading"></div>
    <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
    <orderItem @on-logistics="handlerLogistics" @on-confirm="handlerConfirm" @on-delete="handlerDelete" @on-cancel="handlerCancel" v-for="item in orderList" :key="item.id" :order="item"></orderItem>
    <!-- 分页组件 -->
    <XtxPagination @current-page="reqParams.page=$event" v-if="total>0" :current-page="reqParams.page" :page-size="reqParams.pageSize" :total="total"></XtxPagination>
    <!-- 取消原因组件 -->
    <OrderCancel ref="orderCancelCom"></OrderCancel>
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticsCom"></OrderLogistics>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import orderItem from './components/order-item.vue'
import { confirmOrder, deleteOrder, findOrderList } from '@/api/order'
import OrderCancel from './components/order-cancel.vue'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/order-logistics.vue'

export default {
  name: 'MemberOrder',
  components: { orderItem, OrderCancel, OrderLogistics },
  setup() {
    const activeName = ref('all')
    const loading = ref(false)
    const total = ref(0)

    // 筛选条件
    const orderList = ref([])
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      orderState: 0
    })

    // 筛选条件发生变化重新加载
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    watch(
      reqParams,
      () => {
        getOrderList()
      },
      { immediate: true }
    )

    // 点击选项卡切换选项卡
    const tabClick = ({ index }) => {
      reqParams.page = 1
      reqParams.orderState = index
    }
    // 发请求拿数据
    findOrderList(reqParams).then(data => {
      orderList.value = data.result.items
    })

    // 删除订单
    const handlerDelete = order => {
      Confirm({ text: '确认删除吗？' })
        .then(() => {
          deleteOrder(order.id).then(() => {
            Message({ type: 'success', text: '删除订单成功' })
            getOrderList()
          })
        })
        .catch(() => {})
    }
    return { activeName, orderStatus, orderList, tabClick, reqParams, loading, total, ...useCancel(), handlerDelete, ...useConfirm(), ...useLogistics() }
  }
}
// 取消订单
export const useCancel = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  const handlerCancel = order => {
    orderCancelCom.value.open(order)
  }
  return { handlerCancel, orderCancelCom }
}

// 确认收货
export const useConfirm = () => {
  const handlerConfirm = order => {
    Confirm({ text: '确认收货吗？' })
      .then(() => {
        confirmOrder(order.id).then(() => {
          Message({ type: 'success', text: '确认收货成功' })
          // 订单状态修改 待收货-> 待评价
          order.orderState = 4
        })
      })
      .catch(() => {})
  }
  return { handlerConfirm }
}

// 查看物流
export const useLogistics = () => {
  const orderLogisticsCom = ref(null)
  const handlerLogistics = order => {
    orderLogisticsCom.value.open(order)
  }
  return { handlerLogistics, orderLogisticsCom }
}
</script>

<style scoped lang="less">
.member-order {
  height: 100%;
  background: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
